package com.evamay.kotlindemo.ui.FileIo

import android.app.Activity
import android.graphics.Bitmap
import android.os.Bundle
import android.os.PersistableBundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.evamay.kotlindemo.R
import com.evamay.kotlindemo.entity.TypeInfo
import com.evamay.kotlindemo.service.DownLoadCallBack
import com.evamay.kotlindemo.service.ImagesDownLoadService
import com.evamay.kotlindemo.ui.main.MainAdapter
import com.evamay.kotlindemo.utils.FileUtils
import kotlinx.android.synthetic.main.activity_file.*
import java.io.File

class FilesTestActivity : AppCompatActivity(){
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_file)

        initData()
    }


    private fun initData() {
        var list=ArrayList<TypeInfo>()
        list.add(TypeInfo(1,"创建文件夹"))
        list.add(TypeInfo(2,"创建文本文件"))
        list.add(TypeInfo(3,"编辑text"))
        list.add(TypeInfo(4,"Glide下载图片文件"))
        list.add(TypeInfo(5,"删除单个文件"))
        list.add(TypeInfo(6,"删除多个文件"))

        var layoutManager= LinearLayoutManager(this)
        var mAdapter=MainAdapter(this,list)
        rv_file.layoutManager=layoutManager
        rv_file.adapter=mAdapter
        mAdapter.setClickListener(object :MainAdapter.OnItemClickListener{
            override fun onItemClick(position: Int) {
                when (list[position].type) {
                    1->{
                        createDir("")
                    }
                    2->{
                        createTextFile("测试文本.txt")

                    }
                    3->{
                        editTextFile()
                    }
                    4->{
                        saveImage()
                    }
                    5->{
                        deleteSingleFile()
                    }
                    6->{

                    }
                }
            }

        })
    }

    private fun deleteSingleFile() {

    }

    private fun saveImage() {
        var imgUrl="https://weimikeji.oss-cn-qingdao.aliyuncs.com/hotCategories/%E5%B8%A6%E9%A5%AD.png"
        var downService:ImagesDownLoadService= ImagesDownLoadService(imgUrl,this,object :DownLoadCallBack{
            override fun onDownLoadSuccess(file: File) {

            }

            override fun onDownLoadSuccess(bitmap: Bitmap) {
                runOnUiThread {
                    Glide.with(baseContext).load(bitmap).into(iv_file)
                }


            }

            override fun onDownLoadFailed(message: String) {
                runOnUiThread {
                    Toast.makeText(baseContext,message,Toast.LENGTH_SHORT).show()
                }

            }

        })

        Thread(downService).start()
    }

    private fun editTextFile() {
        FileUtils.writeToFile("Edit the log ",FileUtils.baseFilePath+"text/","test.txt")
    }

    private fun createTextFile(fileName:String) {
//        FileUtils.createFile(FileUtils.baseFilePath+"text/","test.txt")
        var textFilePackage=File(FileUtils.baseFilePath+"text/")
        var textFile=File(FileUtils.baseFilePath+"text/$fileName")
        if (textFilePackage.exists().not()) {
            textFilePackage.mkdirs()
            if (!textFile.exists()) {
                textFile.createNewFile()
                Toast.makeText(this, "文件${fileName}不存在，已为你创建", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "文件${fileName}已存在", Toast.LENGTH_SHORT).show()
            }
        } else {
            if (!textFile.exists()) {
                Toast.makeText(this, "文件${fileName}不存在，已为你创建", Toast.LENGTH_SHORT).show()
                textFile.createNewFile()
            } else {
                Toast.makeText(this, "文件${fileName}已存在", Toast.LENGTH_SHORT).show()
            }
        }
    }

    private fun createDir(path:String) {
        var file=File(FileUtils.baseFilePath+path)
        if (file.exists().not()) {
            file.mkdirs()
            Toast.makeText(this,"文件夹不存在，以为你创建",Toast.LENGTH_SHORT).show()
        } else {
            Toast.makeText(this,"文件夹已存在",Toast.LENGTH_SHORT).show()
        }
    }
}